package com.gking.centerSystem.service.ConfigSwitch;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gking.centerSystem.common.ProgramException;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.ConfigSwitch;
import com.gking.centerSystem.entity.enums.ConfigSwitchCodeEnum;
import com.gking.centerSystem.mapper.ConfigSwitchMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class ConfigSwitchServiceImpl implements ConfigSwitchService {

    @Resource
    private ConfigSwitchMapper configSwitchMapper;


    @Override
    public ConfigSwitch getByCode(String configCode) {
        try {
            LambdaQueryWrapper<ConfigSwitch> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ConfigSwitch::getCode, configCode);
            wrapper.eq(ConfigSwitch::getIsDelete, 0);
            ConfigSwitch configSwitch = configSwitchMapper.selectOne(wrapper);

            if (configSwitch == null) return null;
            return configSwitch;
        } catch (Exception e) {
            log.error("获取系统配置功能状态失败 {} ", e.getMessage());
            throw new ProgramException();
        }

    }


    @Override
    public Result<String> setConfigSwitchStatus(String configCode, Integer status, String disableMsg) {
        try {
            if (configCode == null || status == null)
                return Result.error("请将参数补充完整");
            if (ConfigSwitchCodeEnum.getEnumByCode(configCode) == null)
                return Result.error("configCode参数错误");
            if (status != 1 && status != 0)
                return Result.error("status参数错误");
            if (status == 1 && disableMsg.isEmpty())
                return Result.error("请将禁用信息补充完整完整");

            LambdaUpdateWrapper<ConfigSwitch> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(ConfigSwitch::getCode, configCode);
            wrapper.eq(ConfigSwitch::getIsDelete, 0);
            wrapper.set(ConfigSwitch::getStatus, status);
            wrapper.set(ConfigSwitch::getDisableMsg, disableMsg);

            configSwitchMapper.update(null, wrapper);
            ConfigSwitchCodeEnum enumByCode = ConfigSwitchCodeEnum.getEnumByCode(configCode);
            assert enumByCode != null;
            String remark = enumByCode.getRemark();
            return Result.success("更新" + remark + "状态成功");
        } catch (Exception e) {
            log.error("更新系统配置功能状态失败 {} ", e.getMessage());
            throw new ProgramException();
        }
    }


    @Override
    public Result<Boolean> judgeConfigSwitchStatus(String configCode) {
        try {
            if (ConfigSwitchCodeEnum.getEnumByCode(configCode) == null)
                return Result.error("configCode参数错误");

            LambdaQueryWrapper<ConfigSwitch> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ConfigSwitch::getCode, configCode);
            wrapper.eq(ConfigSwitch::getIsDelete, 0);
            ConfigSwitch configSwitch = configSwitchMapper.selectOne(wrapper);

            if (configSwitch == null) {
                return Result.build(500, "功能code 不存在", false);
            } else {
                if (configSwitch.getStatus() == 0) return Result.success("获取系统配置功能状态成功", true);
                if (configSwitch.getStatus() == 1) return Result.success(configSwitch.getDisableMsg(), false);
            }
            return Result.build(500, "功能code 不存在", false);
        } catch (Exception e) {
            log.error("获取系统配置功能状态失败 {} ", e.getMessage());
            throw new ProgramException();
        }
    }


}
